/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.ps.in;

import wtkx.Application;

import static wtkx.ps.Wtkx.Op.*;

import com.google.protobuf.Message;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Script application loader integrated with 'proto-streamer' for 'wtkx' protocol.
 *
 * @author jdp
 */
public abstract class Script
    extends wtkx.in.Script
    implements wtkx.ps.Consumer
{

    public Script(){
        super();
    }
    protected Script(Class<? extends Application> next){
        super(next);
    }


    public void clientConnected(wtkx.ps.Client client){
    }
    public void clientDisconnected(wtkx.ps.Client client){
    }
    public boolean clientUpdate(wtkx.ps.Client client, Message m){
        if (m instanceof wtkx.ps.Wtkx.Update){
            wtkx.ps.Wtkx.Update wm = (wtkx.ps.Wtkx.Update)m;
            if (wm.hasOp()){
                switch (wm.getOp()){
                case OpenByPath:
                    try {
                        this.updateOpen(wm.getId(),wm.getPath());
                    }
                    catch (IOException exc){
                        exc.printStackTrace();
                    }
                    return true;
                case OpenByUrl:
                    try {
                        this.updateOpen(wm.getId(),new URL(wm.getUrl()));
                    }
                    catch (MalformedURLException exc){
                        exc.printStackTrace();
                    }
                    catch (IOException exc){
                        exc.printStackTrace();
                    }
                    return true;
                case OpenWithContent:
                    try {
                        this.updateOpen(wm.getId(),wm.getContent().newInput());
                    }
                    catch (IOException exc){
                        exc.printStackTrace();
                    }
                    return true;
                case UpdateAppend:
                    this.updateAppend(wm.getId(),wm.getAt(),wm.getWith());
                    return true;
                case UpdateInsert:
                    this.updateInsert(wm.getId(),wm.getAt(),wm.getStart(),wm.getWith());
                    return true;
                case UpdateReplace:
                    this.updateReplace(wm.getId(),wm.getAt(),wm.getSearch(),wm.getWith());
                    return true;
                case UpdateDelete:
                    this.updateDelete(wm.getId(),wm.getAt(),wm.getStart(),wm.getEnd());
                    return true;
                case Close:
                    this.updateClose(wm.getId());
                    return true;
                case Heartbeat:
                    return true;
                default:
                    return false;
                }
            }
            else
                throw new IllegalStateException();
        }
        else
            return false;
    }
}
